package com.bonc.ioc.service.impl;

import com.bonc.ioc.base.page.PageData;
import com.bonc.ioc.base.page.PageResult;
import com.bonc.ioc.base.service.BaseService;
import com.bonc.ioc.dao.StandardBasicDataMapper;
import com.bonc.ioc.dao.StandardDetailMapper;
import com.bonc.ioc.service.StandardBasicDataService;
import com.bonc.ioc.service.StandardDetailService;
import com.bonc.ioc.vo.SaveStandardDetailVo;
import com.bonc.ioc.vo.StandardBasicDataVo;
import com.bonc.ioc.vo.StandardDetailVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @version 1.0
 * @description:
 * @author: wangqinglin
 * @date: 2021/3/26 14:20
 * @change : 2021/3/26 14:20 by q8541q@163.com  for init
 */
@Service
public class StandardDetailServiceImpl extends BaseService implements StandardDetailService {

    @Resource
    private StandardDetailMapper standardDetailMapper;

    @Autowired
    private StandardBasicDataMapper standardBasicDataMapper;

    @Override
    public int deleteByPrimaryKey(String id) {
        return standardDetailMapper.delete(id);
    }

    @Override
    public int save(SaveStandardDetailVo record) {


        if (record.getId() != null) {
            List<StandardDetailVo> kv = record.getKv();
            for (StandardDetailVo s : kv) {
                StandardBasicDataVo standardBasicDataVo = standardBasicDataMapper.selectByPrimaryKey(s.getBdId());
                if (s.getContent().length() > Integer.valueOf(standardBasicDataVo.getLimits())) {
                    s.setContent(s.getContent().substring(0, Integer.valueOf(standardBasicDataVo.getLimits())));//截取
                }

                fillModifyTimeInfo(s);
            }
            if (kv.size() == 0) return 0;
            else return standardDetailMapper.saveBatch(kv);
        } else {
            List<StandardDetailVo> kv = record.getKv();
            Long stId = record.getStId();
            for (StandardDetailVo s : kv) {
                StandardBasicDataVo standardBasicDataVo = standardBasicDataMapper.selectByPrimaryKey(s.getBdId());
                if (s.getContent().length() > Integer.valueOf(standardBasicDataVo.getLimits())) {
                    s.setContent(s.getContent().substring(0, Integer.valueOf(standardBasicDataVo.getLimits())));//截取
                }
                //String uuid = UUID.randomUUID().toString();
                //s.setRecordId(uuid);
                s.setStId(stId);
                fillBasicTimeInfo(s);
                fillModifyTimeInfo(s);
            }
            if (kv.size() == 0) return 0;
            else return standardDetailMapper.saveBatch(kv);


        }
    }

    @Override
    public StandardDetailVo selectByPrimaryKey(Long id) {
        return standardDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(StandardDetailVo record) {
        return standardDetailMapper.update(record);
    }

    @Override
    public SaveStandardDetailVo getStandDetailInfo(String id) {
        return standardDetailMapper.getStandDetailInfo(id);
    }

    @Override
    public List<SaveStandardDetailVo> getStandDetailInfoList(String id) {
        return standardDetailMapper.getStandDetailInfoList(id);
    }

    private void fillModifyTimeInfo(StandardDetailVo record) {
        record.setModifyTime(new Date());
    }

    private void fillBasicTimeInfo(StandardDetailVo record) {
        record.setCreateTime(new Date());
    }

    @Override
    public PageResult getStandDetailInfoListByPage(PageData pageData) throws Exception {
        getPageParams();
        PageResult pageResult = new PageResult(pageData, standardDetailMapper.selectByPage(pageData));
        LinkedList linkedList = new LinkedList<>();
        List<Map> rows = pageResult.getRows();
        if (pageData.containsKey("query") && StringUtils.isNotBlank(pageData.get("query").toString())) {
            String query = pageData.get("query").toString();
            if (rows.size() > 0) {
                for (int i = 0; i < rows.size(); i++) {
                    SaveStandardDetailVo standardDetailVo = new SaveStandardDetailVo();
                    standardDetailVo = (SaveStandardDetailVo) rows.get(i);
                    List<StandardDetailVo> kv = standardDetailVo.getKv();
                    if (kv.size() > 0) {
                        if (kv.stream().filter(s1 -> {
                            if (StringUtils.isNotBlank(s1.getContentValue())) {
                                return s1.getContentValue().contains(query);
                            } else {
                                return false;
                            }
                        }).count() > 0) {
                            linkedList.add(rows.get(i));
                        }
                    }
                }
                if (linkedList.size() == 0) {
                    SaveStandardDetailVo saveStandardDetailVo = (SaveStandardDetailVo) rows.get(0);
                    List<StandardDetailVo> kv = saveStandardDetailVo.getKv();
                    kv.stream().forEach(ss -> {
                        ss.setContentValue("");
                    });
                    linkedList.add(saveStandardDetailVo);
                    pageResult.setTotal(0);
                } else {

                    pageResult.setTotal(linkedList.size());
                }
                pageResult.setRows(linkedList);
            }
            return pageResult;
        } else {
            return pageResult;
        }

    }


}

